package com.itcast.common.config;

import no.api.freemarker.java8.Java8ObjectWrapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;

/**
 * @author itcast
 * @create 2024-09-19 19:32
 */
@Component
public class FreeMarkerConfig {

    @Configuration
    public class FreemarkerConfig implements BeanPostProcessor {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof FreeMarkerConfigurer) {
                FreeMarkerConfigurer configurer = (FreeMarkerConfigurer) bean;
                //解决FreeMarker适配java8中时间格式的问题，能够格式化LocalDateTime
                configurer.getConfiguration().setObjectWrapper(new Java8ObjectWrapper(freemarker.template.Configuration.VERSION_2_3_32));
            }
            return bean;
        }
    }
}
